package com.shen.langchain4j.controller;

import cn.hutool.core.date.DateUtil;
import com.shen.langchain4j.service.ChatAssistant;
import com.shen.langchain4j.service.ChatMemoryAssistant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/memoryChat")
public class ChatMemoryController {
    @Autowired
    @Qualifier("chat")
    private ChatAssistant chat;

    @Autowired
    @Qualifier("chatMemoryWithMessageWindow")
    private ChatMemoryAssistant chatMemoryWithMessageWindow;

    @Autowired
    @Qualifier("chatMemoryWithTokenWindow")
    private ChatMemoryAssistant chatMemoryWithTokenWindow;

    /**
     * 普通对话
     *
     * @return 调用结果
     */
    @GetMapping(value = "/chatOne")
    public String chatOne() {
        String answerOne = chat.chat("你好，我的名字叫张三");
        log.info("answerOne: {}", answerOne);
        String answerTwo = chat.chat("我的名字叫什么");
        log.info("answerTwo: {}", answerTwo);
        return "success : " + DateUtil.now() + "<br> \n\n answerOne: " + answerOne + "<br> \n\n answerTwo: " + answerTwo;
    }

    /**
     * 基于保留消息数的滑动窗口淘汰策略的带有记忆对话
     *
     * @return 调用结果
     */
    @GetMapping(value = "/chatTwo")
    public String chatTwo() {
        chatMemoryWithMessageWindow.chatWithMemory(1L, "你好，我的名字叫小明");
        String answerOne = chatMemoryWithMessageWindow.chatWithMemory(1L, "我的名字叫什么");
        log.info("answerOne: {}", answerOne);

        chatMemoryWithMessageWindow.chatWithMemory(3L, "你好，我的名字叫小红");
        String answerTwo = chatMemoryWithMessageWindow.chatWithMemory(3L, "我的名字叫什么");
        log.info("answerTwo: {}", answerTwo);
        return "chatMemoryWithMessageWindow success : " + DateUtil.now() + "<br> \n\n answerOne: " + answerOne + "<br> \n\n answerTwo: " + answerTwo;
    }

    /**
     * 基于保留Token数的滑动窗口淘汰策略的带有记忆对话
     *
     * @return 调用结果
     */
    @GetMapping(value = "/chatThree")
    public String chatThree() {
        chatMemoryWithTokenWindow.chatWithMemory(1L, "你好，我的名字叫小明");
        String answerOne = chatMemoryWithTokenWindow.chatWithMemory(1L, "我的名字叫什么");
        log.info("answerOne: {}", answerOne);

        chatMemoryWithTokenWindow.chatWithMemory(5L, "你好，我的名字叫小红");
        String answerTwo = chatMemoryWithTokenWindow.chatWithMemory(5L, "我的名字叫什么");
        log.info("answerTwo: {}", answerTwo);
        return "chatMemoryWithTokenWindow success : " + DateUtil.now() + "<br> \n\n answerOne: " + answerOne + "<br> \n\n answerTwo: " + answerTwo;
    }
}
